<? 
		if(isset($_POST['btnsubmit']))
		{
			require("required/dbconnect.php"); 
			
			$stock_amount=$_POST['stock_amount'];
			$current_amount=$_POST['current_amount'];
			$id=$_POST['hidid'];
			$remarks=$_POST['remarks'];			
			
			if($current_amount>$stock_amount){
				$deducted=$current_amount-$stock_amount;
				$system_remark=$deducted." item(s) deducted from stock";
			}else{
				$added=$stock_amount-$current_amount;
				$system_remark=$added." item(s) added to stock";
			}
			
			
			$update=mysql_query("update os_products set stock_amount=$stock_amount where id=$id")or die(mysql_error());
			$insert=mysql_query("insert into os_stockrecord (pid,total_added,user_remarks,system_remarks,add_datetime)values($id,$stock_amount,'$remarks','$system_remark',now())")or die(mysql_error());
		
			header("Location:product_viewfull.php?id=$id&updated=3");
	
		}
		else
		{

				require("required/dbconnect.php");
				require("required/function.php");  
				require("required/header.php"); 
				$id=$_GET['id'];
				$get_id=mysql_query("select * from os_products where id=$id");
			
				$a=mysql_fetch_array($get_id,MYSQL_ASSOC);
				
				

?>
<script language="javascript">
	function validatefrm(frm)
	{
		if(document.frm.stock_amount.value=="")
		{
			alert("Please select total stock");
			document.frm.stock_amount.focus();
			return false;
		}
		
		return true;
	
	}
</script>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
	<script language="JavaScript" type="text/javascript" src="wysiwyg.js">
	</script>
	<script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
	
	<td class="solidborder" width="84%" align="left" valign="top">
		<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" onSubmit="return validatefrm(this)">
			<table width="100%">
				<td class="title" colspan="2"><strong>Update Stock</strong></td>
				
				<tr>
					<td width="30%">Current Stock Available for <strong><?= $a['product_name_eng'] ?></strong></td><td width="70%"><input type="text" name="stock_amount" onkeyup="removeAlphabet(this.name)" value="<?= $a['stock_amount'] ?>" size="3" maxlength="5"> Item(s)</td>
				</tr>
				<tr>
					<Td>Remark</Td><td><input type="text" size="50" name="remarks"></td>
				</tr>
				
				<input type="hidden" value="<?= $a['stock_amount'] ?>" name="current_amount">
				<input type="hidden" value="<?= $id ?>" name="hidid">
				<tr>
					<td colspan="2"><input type="submit" class="button" value="Submit" name="btnsubmit"> <input type="button" onClick="history.go(-1)" class="button" value="Back"></td>
				</tr>
				<tr>
					<td colspan="2">
					<br><br>
					<hr>
						<strong>Stock Update History</strong>
						<table width="100%">
							<tr class="title_bar">
								<td width="5%">No</td><td width="39%">Updated Stock</td><Td width="40%">Remarks</Td><td width="16%">Date</td>
							</tr>
							<?
								$get=mysql_query("select * from os_stockrecord where pid=$id order by add_datetime desc")or die(mysql_error());
							
								if(mysql_num_rows($get)==0){
							?>
							<Tr>
								<td colspan="4" align="center"> - no record - </td>
							</Tr>
							<? 
								}else{ 
									$no=1;
									while($b=mysql_fetch_array($get,MYSQL_ASSOC)){
										if($no%2==0)
										{
											$bgcolor="#e5e5e5";
										}
										else
										{
											$bgcolor="#eeeeee";	
										}
										
										if($b['bid']!=0)
										{
											$get_bill=mysql_query("select * from os_bill_details where id=".$b['bid']);
											
											if(mysql_num_rows($get_bill)>0)
											{
												$bill=mysql_fetch_array($get_bill,MYSQL_ASSOC);
												$bid=$bill['id'];
												$invoice="<a href='order_viewfull.php?id=$bid'>".$bill['invoice_number']."</a>";
											}else{
												$invoice="unknown invoice";
											}
											$remark="[System Generated] from Invoice ".$invoice;
										}else{
											$remark=$b['user_remarks'];
										}
										
								
							?>
							 <tr height="20" bgcolor="<?= $bgcolor ?>" onmouseover="this.style.backgroundColor='ccccff'" onmouseout="this.style.backgroundColor='<?= $bgcolor ?>'">
								<td><?= $no ?></td>
								<td><?= $b['total_added'] ?>(<?= $b['system_remarks'] ?>)</td>
								<td><?= $remark ?></td>
								<td><?= $b['add_datetime'] ?></td>
							</tr>
							
							<? $no++; }} ?>
						
						</table>
					
					</td>
				</tr>
			</table>
		</form>
	</td>
 
</table>
<? }require("required/footer.php"); ?>